/**
 * 获取url后面的参数
 * @param name 参数名称
 * @returns {*}
 * @constructor
 */
function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

/**
 * 判断对象是否为空
 * @param obj
 */
function isEmpty(obj) {
    if (obj == "" || obj == null || obj == undefined) return true;
}

/*
   http 请求封装
   let url = 'http://demo.com/api'
    例:
        fetchHttp(url).
            then( res => {
                console.log(res)
            }).catch( e => {
                console.log(e)
            })
        fetchHttp(url, {
            method: 'POST',
        })
*/
function fetchHttp(url, setting = {}) {
    //设置参数的初始值
    let opts = {
        method: (setting.method || 'GET').toUpperCase(), //请求方式
        headers: setting.headers || {}, // 请求头设置
        credentials: setting.credentials || true, // 设置cookie是否一起发送
        body: setting.body || {},
        mode: setting.mode || 'cors', // 可以设置 cors, no-cors, same-origin
        redirect: setting.redirect || 'follow', // follow, error, manual
        cache: setting.cache || 'default' // 设置 cache 模式 (default, reload, no-cache)
    }
    let dataType = setting.dataType || "json", // 解析方式
        data = setting.data || "" // 参数

    // 参数格式化
    function params_format(obj) {
        var str = ''
        for (var i in obj) {
            str += `${i}=${obj[i]}&`
        }
        return str.split('').slice(0, -1).join('')
    }

    if (opts.method === 'GET') {
        url = url + (data ? `?${params_format(data)}` : '')
    } else {
        setting.body = data || {}
    }
    return new Promise((resolve, reject) => {
        fetch(url, opts).then(async res => {
            let data = dataType === 'text' ? await res.text() :
                dataType === 'blob' ? await res.blob() : await res.json()
            resolve(data)
        }).catch(e => {
            reject(e)
        })
    })
}